package team.fivez.defaultmanage.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import team.fivez.defaultmanage.dao.*;
import team.fivez.defaultmanage.domain.Customer;
import team.fivez.defaultmanage.domain.DefaultApplication;
import team.fivez.defaultmanage.mediumDomain.CompletedDefaultApplication;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Component
public class DefaultApplicationService {

    private final DefaultApplicationDao defaultApplicationDao;
    private final CustomerDao customerDao;
    private final DefaultReasonDao defaultReasonDao;
    private final HaveDefaultReasonDao haveDefaultReasonDao;

    private final ApproverDao approverDao;

    @Autowired
    public DefaultApplicationService(DefaultApplicationDao defaultApplicationDao, CustomerDao customerDao,
                                     DefaultReasonDao defaultReasonDao, HaveDefaultReasonDao haveDefaultReasonDao, ApproverDao approverDao) {
        this.defaultApplicationDao = defaultApplicationDao;
        this.customerDao = customerDao;
        this.defaultReasonDao = defaultReasonDao;
        this.haveDefaultReasonDao = haveDefaultReasonDao;
        this.approverDao = approverDao;
    }


    //新增违约申请，0成功，1失败
    public int addNewDefaultApplication(CompletedDefaultApplication completedDefaultApplication) throws InterruptedException {
        //拆分中间层类
        DefaultApplication defaultApplication = completedDefaultApplication.getDefaultApplication();
        List<String> defaultReasonNumberList = completedDefaultApplication.getDefaultReasonList();

        //确认该客户是不是存在
        Integer count = customerDao.selectCountByName(defaultApplication.getCustomerName());
        if(count!=1){
            return 1;
        }

        //确认该客户是否已经在违约状态
        Customer customerFromDB = customerDao.getCustomerByName(defaultApplication.getCustomerName());
        if(customerFromDB.getDefaultStatus()==1){
            return 1;
        }

        //确认该客户是否已经有申请单
        Integer countByName = defaultApplicationDao.selectCountByName(defaultApplication.getCustomerName());
        if(countByName==1){
            return 1;
        }




        //违约单信息完备
        String applicationNumber="DAN"+System.currentTimeMillis();//生成违约申请单号
        Thread.sleep(2);
        defaultApplication.setApplicationNumber(applicationNumber);
        defaultApplication.setApplicationTime(new Timestamp(System.currentTimeMillis()));

        //由于具有多个原因，进行插入
        for (String s : defaultReasonNumberList) {
            haveDefaultReasonDao.insertOneHave(applicationNumber, Integer.parseInt(s));
        }

        //插入申请单
        int i = defaultApplicationDao.insertDefaultApplication(defaultApplication);
        if(i==1) return 0;
        else return 1;

    }

    //申请人的违约清单页面的初始化
    public List<CompletedDefaultApplication> listSelectedCustomerByApplicant(String applicantAccount){

        //找到所有的申请单
        List<DefaultApplication> listByApplicantAccount = defaultApplicationDao.getListByApplicantAccount(applicantAccount);
        //最终返回的列表
        return completeDefaultApplicationList(listByApplicantAccount);

    }

    //申请人根据客户名进行查找
    public List<CompletedDefaultApplication> listSelectedCustomerByApplicantAndCustomerName(String applicantAccount,String customerNameO){

        //找到申请单
        List<DefaultApplication> listByName = defaultApplicationDao.getListByCustomerNameAndApplicantAccount(customerNameO, applicantAccount);

        //最终返回的列表
        return completeDefaultApplicationList(listByName);
    }

    //申请人根据审核状态进行查找
    public List<CompletedDefaultApplication> listSelectedCustomerByApplicantAndStatus(String applicantAccount, int status){

        //找到申请单
        List<DefaultApplication> listByStatus = defaultApplicationDao.getListByApprovementStatusAndApplicantAccount(status, applicantAccount);

        //最终返回的列表
        return completeDefaultApplicationList(listByStatus);
    }


    //审核人的初始化列表
    public List<CompletedDefaultApplication> listApplication(){
        List<DefaultApplication> allList = defaultApplicationDao.getAllList();

        return completeDefaultApplicationList(allList);
    }


    //审核人按照客户名称查询
    public List<CompletedDefaultApplication> listApplicationByCustomerName(String customerName){
        List<DefaultApplication> listByCustomerName = defaultApplicationDao.getListByCustomerName(customerName);

        return completeDefaultApplicationList(listByCustomerName);
    }


    //审核人按照审核状态进行查询
    public List<CompletedDefaultApplication> listApplicationByStatus(int status){
        List<DefaultApplication> listByApprovementStatus = defaultApplicationDao.getListByApprovementStatus(status);

        return completeDefaultApplicationList(listByApprovementStatus);
    }


    //完善所有的违约申请表中的信息
    public List<CompletedDefaultApplication> completeDefaultApplicationList(List<DefaultApplication> list) {
        List<CompletedDefaultApplication> completedList = new ArrayList<>();
        for (DefaultApplication defaultApplication : list) {

            //获取申请单号
            String applicationNumber = defaultApplication.getApplicationNumber();

            List<Integer> reasonNumberList = haveDefaultReasonDao.selectAllReasonByApplicationNumber(applicationNumber);

            //原因列表
            List<String> defaultReasonList = new ArrayList<>();
            for (Integer number : reasonNumberList) {
                defaultReasonList.add(defaultReasonDao.getDefaultReasonByNumber(number).getDefaultReason());
            }

            //获取客户名字
            String customerName = defaultApplication.getCustomerName();
            Customer customerInfo = customerDao.getCustomerByName(customerName);

            completedList.add(new CompletedDefaultApplication(customerInfo,defaultApplication,defaultReasonList));


        }

        return completedList;
    }

    //更新审核状态
    public int updateStatus(int status,String applicationNumber,String approverAccount){
        DefaultApplication defaultApplication = defaultApplicationDao.getDefaultApplicationByNumber(applicationNumber);
        if(defaultApplication.getApprovementStatus()!=0){
            return 1;//失败
        }
        if (status==2){
            defaultApplicationDao.updateApprovementStatusByApplicationNumber(status,applicationNumber);
            //更新审核人姓名
            defaultApplicationDao.updateDefaultApproverNameByApplicationNumber(applicationNumber,
                    approverDao.getApproverByApproverAccount(approverAccount).getApproverName());
            //更新审核时间
            defaultApplicationDao.updateApplicationTimeByApplicationNumber(applicationNumber,
                    new Timestamp(System.currentTimeMillis()));
        } else if (status==1) {//同意违约申请单就是将用户改为违约状态

            defaultApplicationDao.updateApprovementStatusByApplicationNumber(status,applicationNumber);
            customerDao.updateStatusByCustomerName(defaultApplication.getCustomerName(),1);
            //更新审核人姓名
            defaultApplicationDao.updateDefaultApproverNameByApplicationNumber(applicationNumber,
                    approverDao.getApproverByApproverAccount(approverAccount).getApproverName());
            //更新审核时间
            defaultApplicationDao.updateApplicationTimeByApplicationNumber(applicationNumber,
                    new Timestamp(System.currentTimeMillis()));
        }

        return 0;//成功

    }




}
